package com.zb.knowledge.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zb.entity.SelectResultVo;
import com.zb.entity.ZjInstructionBook;
import com.zb.entity.ZjKnowledgeTypeInstructionBook;
import com.zb.knowledge.service.ContInstructionBookService;
import com.zb.service.ZjInstructionBookService;
import com.zb.service.ZjKnowledgeTypeInstructionBookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author WEN
 * @Date 2023/8/14 0014
 **/
@Service
public class ContInstructionBookServiceImpl implements ContInstructionBookService {
    @Resource
    private ZjKnowledgeTypeInstructionBookService knowledgeTypeInstructionBookService;
    @Resource
    private ZjInstructionBookService instructionBookService;

    // 批量删除说明书
    @Override
    public void deleteByIds(List<Long> idList) {
        knowledgeTypeInstructionBookService.deleteBatchByInstructionBookId(idList);
        instructionBookService.removeByIds(idList);
    }

    // 根据知识库分类ID获取说明书
    @Override
    public Object getInstructionBook(Long knowledgeTypeId) {
        QueryWrapper<ZjKnowledgeTypeInstructionBook> instructionBookQueryWrapper = new QueryWrapper<>();
        instructionBookQueryWrapper.select("knowledge_type_id", "instruction_book_id");
        instructionBookQueryWrapper.eq("knowledge_type_id", knowledgeTypeId);
        List<ZjKnowledgeTypeInstructionBook> instructionBookList = knowledgeTypeInstructionBookService.list(instructionBookQueryWrapper);
        if (CollectionUtil.isEmpty(instructionBookList)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjInstructionBook> bookQueryWrapper = new QueryWrapper<>();
        bookQueryWrapper.select("id", "name", "image");
        bookQueryWrapper.in("id", instructionBookList.stream().map(ZjKnowledgeTypeInstructionBook::getInstructionBookId).toList());
        List<ZjInstructionBook> bookList = instructionBookService.list(bookQueryWrapper);
        return bookList.stream().map(book -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", book.getId());
            map.put("name", book.getName());
            map.put("image", book.getImage());
            return map;
        }).toList();
    }
}
